package com.xywh.micro_warehousing.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.xywh.micro_warehousing.R;
import com.xywh.micro_warehousing.di.component.DaggerSaoMiaoChaKanKuCunComponent;
import com.xywh.micro_warehousing.di.module.SaoMiaoChaKanKuCunModule;
import com.xywh.micro_warehousing.mvp.contract.SaoMiaoChaKanKuCunContract;
import com.xywh.micro_warehousing.mvp.presenter.SaoMiaoChaKanKuCunPresenter;
import com.xywh.micro_warehousing.mvp.ui.entity.OpenLockAisle;
import com.xywh.micro_warehousing.mvp.ui.entity.SaoMiaoChaKanKuCun;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.base.BaseConstants.SCAN_REQUEST_CODE;
import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.xywh.micro_warehousing.mvp.ui.fragment.Tab2Fragment.REQUEST_WU_LIAO;


@Route(path = RouterHub.ACTIVITY_SAO_MIAO_CHA_KAN_KU_CUN)
public class SaoMiaoChaKanKuCunActivity extends BaseActivity<SaoMiaoChaKanKuCunPresenter> implements SaoMiaoChaKanKuCunContract.View {


    @BindView(R.id.btn_scan)
    Button btnScan;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_cang_wei)
    TextView tvCangWei;
    @BindView(R.id.tv_dan_wei)
    TextView tvDanWei;
    private String scan_result;
    private String mCounterId = "";
    private String mCangWeiHao = "";
    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSaoMiaoChaKanKuCunComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .saoMiaoChaKanKuCunModule(new SaoMiaoChaKanKuCunModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_sao_miao_cha_kan_ku_cun; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_sao_miao_cha_kan_ku_cun));

        try {
            scan_result = getIntent().getExtras().getString("scan_result");
            if (!TextUtils.isEmpty(scan_result)) {
                getSaoMiaoChaKanKuCunData(scan_result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void getSaoMiaoChaKanKuCunData(String EslNumber) {
        HashMap<String, String> map = new HashMap<>();
        map.put("EslNumber", EslNumber);
        mPresenter.getSaoMiaoChaKanKuCunData(map);
    }

    @Override
    public void getSaoMiaoChaKanKuCunDataSuccess(SaoMiaoChaKanKuCun entityData) {

        try {
            showMessage(entityData.getMessage());
            if (entityData != null) {

                //TODO mCounterId不确定是哪个
                mCounterId = entityData.getData().getCounterId();
                mCangWeiHao = entityData.getData().getAisleShortNo();
                tvName.setText(entityData.getData().getMaterial());
                tvNum.setText(entityData.getData().getNum());
                tvCangWei.setText(entityData.getData().getAisleShortNo());
                tvDanWei.setText(entityData.getData().getNumUnit());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void openLockAisleSuccess(OpenLockAisle entityData) {
        if (entityData.getCode() == 200) {
            showMessage("开锁成功");
        } else if (entityData.getCode() == 400) { //仓储柜已停用
            showMessage(entityData.getMessage());
        } else {
            showMessage("开锁失败");
        }
    }

    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @OnClick(R.id.btn_scan)
    public void onViewClicked() {
        toScan();
    }

    private void toScan() {

        Intent newIntent = new Intent(this, ScanActivity.class);
        mSharedPreferencesHelper.putInt(SCAN_REQUEST_CODE, REQUEST_WU_LIAO);
        startActivityForResult(newIntent, REQUEST_WU_LIAO);

        tvName.setText("");
        tvNum.setText("");
        tvCangWei.setText("");
    }

    @OnClick(R.id.btn_kai_men)
    public void onKaiMenClicked() {

        if (!TextUtils.isEmpty(mCounterId) && !TextUtils.isEmpty(mCangWeiHao)) {
            showMessageNegativeDialog();
        } else {
            showMessage("请先扫描仓位二维码确认物料");
        }

    }

    private void showMessageNegativeDialog() {
        new QMUIDialog.MessageDialogBuilder(getContext())
                .setTitle("确认开门")
                .setMessage("确认开门领取物料？")
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, "确定", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("spid", mCounterId);
                        map.put("aisleNo", mCangWeiHao);
//                        map.put("uid", mSharedPreferencesHelper.getString(BaseConstants.UID));
                        mPresenter.openLockAisle(map);
                        dialog.dismiss();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == REQUEST_WU_LIAO) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                String eslNumber = extras.getString(BaseConstants.SCAN_RESULT);
                getSaoMiaoChaKanKuCunData(eslNumber);

            }

        }
    }


}
